package com.anxin.converter;

import com.anxin.entity.PatientInfo;
import com.anxin.entity.excel.PatientInfoExcel;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 患者信息转换器
 *
 */
@Component
public class PatientInfoConverter {

    public PatientInfoExcel toExcel(PatientInfo patientInfo) {
        if (patientInfo == null) {
            return null;
        }

        PatientInfoExcel excel = new PatientInfoExcel();
        excel.setId(patientInfo.getId());
        excel.setUserId(patientInfo.getUserId());
        excel.setIdCard(patientInfo.getIdCard());
        excel.setGender(patientInfo.getGender() == 1 ? "男" : patientInfo.getGender() == 2 ? "女" : "未知");
        excel.setBirthDate(Date.from(patientInfo.getBirthDate().atStartOfDay(ZoneId.systemDefault()).toInstant()));
        excel.setAddress(patientInfo.getAddress());
        excel.setEmergencyContact(patientInfo.getEmergencyContact());
        excel.setEmergencyPhone(patientInfo.getEmergencyPhone());
        excel.setAllergicHistory(patientInfo.getAllergicHistory());
        excel.setMedicalHistory(patientInfo.getMedicalHistory());
        excel.setCreateTime(Date.from(patientInfo.getCreateTime().atZone(ZoneId.systemDefault()).toInstant()));
        excel.setUpdateTime(patientInfo.getUpdateTime() != null ?
            Date.from(patientInfo.getUpdateTime().atZone(ZoneId.systemDefault()).toInstant()) : null);

        return excel;
    }

    public PatientInfo toEntity(PatientInfoExcel excel) {
        if (excel == null) {
            return null;
        }

        PatientInfo patientInfo = new PatientInfo();
        patientInfo.setId(excel.getId());
        patientInfo.setUserId(excel.getUserId());
        patientInfo.setIdCard(excel.getIdCard());
        patientInfo.setGender("男".equals(excel.getGender()) ? 1 : "女".equals(excel.getGender()) ? 2 : 0);
        patientInfo.setBirthDate(excel.getBirthDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
        patientInfo.setAddress(excel.getAddress());
        patientInfo.setEmergencyContact(excel.getEmergencyContact());
        patientInfo.setEmergencyPhone(excel.getEmergencyPhone());
        patientInfo.setAllergicHistory(excel.getAllergicHistory());
        patientInfo.setMedicalHistory(excel.getMedicalHistory());
        patientInfo.setCreateTime(excel.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
        patientInfo.setUpdateTime(excel.getUpdateTime() != null ?
            excel.getUpdateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime() : null);

        return patientInfo;
    }

    public List<PatientInfoExcel> toExcelList(List<PatientInfo> patientInfoList) {
        return patientInfoList.stream()
                .map(this::toExcel)
                .collect(Collectors.toList());
    }

    public List<PatientInfo> toEntityList(List<PatientInfoExcel> excelList) {
        return excelList.stream()
                .map(this::toEntity)
                .collect(Collectors.toList());
    }
}
